import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import router from './router'
import { store } from './store'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import App from './App.vue'
import {formatRoutes} from "./http/menu";
import 'element-plus/theme-chalk/index.css';
import {getRequest} from "./http";
router.beforeEach((to, from, next)=>{
	if(to.path == '/'){
		next();
	}else{
		if(sessionStorage.getItem('tokenStr')){
			if(store.state.routes.length == 0){
				getRequest('data/menu.json').then(resp=>{
					if(resp){
						let fmtRoutes = formatRoutes(resp.data);
						fmtRoutes.forEach(r=>{
							router.addRoute(r);
						});

						store.commit('initialRoutes', fmtRoutes);
						next({...to, replace:true});
					}
				});
			}else{
				next();
			}
		}else{
			next('/?redirect='+to.path);
		}
	}
});

const app = createApp(App);
for(const [key, component] of Object.entries(ElementPlusIconsVue)){
	app.component(key, component);
}
app.use(store)
	.use(ElementPlus)
	.use(router)
	.mount('#app');
